package com.xuecheng.framework.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;


import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

public class FeignClientIntercepter implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
//        获取请求头内容
        ServletRequestAttributes requestAttributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if(headerNames==null){
            return;
        }
        while (headerNames.hasMoreElements()){
            String nextElement = headerNames.nextElement();
            String value = request.getHeader(nextElement);
//        将请求头内容向下传递
            template.header(nextElement,value);
        }
    }
}
